<template>
  <a-modal
    :title="title"
    :width="800"
    :ok=false
    :visible="visible"
    :confirmLoading="confirmLoading"
    :okButtonProps="{ props: {disabled: disableSubmit} }"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="企业名称">
          <a-input placeholder="请输入企业名称" v-decorator="['enterpriseName', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="企业名缩写">
          <a-input v-decorator="[ 'enterpeiseShort', {}]" />
        </a-form-item>
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :hidden="seen" label="上级企业" hasFeedback>
          <a-tree-select
            style="width:100%"
            :dropdownStyle="{maxHeight:'200px',overflow:'auto'}"
            :treeData="departTree"
            v-model="model.parentId"
            placeholder="请选择上级企业"
            :disabled="condition">
          </a-tree-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="统一社会信用代码">
          <a-input placeholder="请输入统一社会信用代码" v-decorator="['enterpriseUscc', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="企业代码">
          <a-input placeholder="请输入企业代码" v-decorator="['enterpriseCode', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="负责人姓名">
          <a-input placeholder="请输入负责人姓名" v-decorator="['contactPerson', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="负责人手机">
          <a-input placeholder="请输入负责人手机" v-decorator="['contactPhone', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="企业类型">
          <template >
            <a-radio-group v-decorator="['enterpriseType',validatorRules.enterpriseType]" placeholder="请选择企业类型">
              <a-radio value="0">
                集团
              </a-radio>
              <a-radio value="1">
                企业
              </a-radio>
              <a-radio value="2">
                新势力
              </a-radio>
            </a-radio-group>
          </template>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="管理类型">
          <template >
            <a-radio-group v-decorator="['manageType',validatorRules.manageType]" placeholder="请选择管理类型">
              <a-radio value="0">
                直销
              </a-radio>
              <a-radio value="1">
                代理
              </a-radio>
            </a-radio-group>
          </template>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="代理品牌">
          <a-input placeholder="请输入代理品牌" v-decorator="['brands', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="注册地址">
          <a-input placeholder="请输入注册地址" v-decorator="['registerAddr', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="经营地址">
          <a-input placeholder="请输入经营地址" v-decorator="['operationAddr', {}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import { queryEnterpriseList } from '@/api/api'
import pick from 'lodash.pick'
import ATextarea from 'ant-design-vue/es/input/TextArea'
import { USER_INFO } from '@/store/mutation-types'
import Vue from 'vue'

export default {
  name: "CxmEnterpriseModal",
  components: { ATextarea },
  data () {
    return {
      departTree:[],
      orgTypeData:[],
      phoneWarning:'',
      departName:"",
      usccPath:'',
      title:"操作",
      seen:false,
      visible: false,
      condition:true,
      disableSubmit:false,
      model: {},
      menuhidden:false,
      menuusing:true,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules:{
        enterpriseType:{rules: [{ required: true, message: '请选择企业类型!' }]},
        manageType:{rules: [{ required: true, message: '请选择管理类型!' }]},
        mobile:{rules: [{validator:this.validateMobile}]}
      },
      url: {
        add: "/cxm/cxmEnterprise/add",
        edit: "/cxm/cxmEnterprise/edit",
      },
      dictDisabled:true,
    }
  },
  created () {
  },
  methods: {
    loadTreeData(){
      var that = this;
      queryEnterpriseList().then((res)=>{
        if(res.success){
          that.departTree = [];
          for (let i = 0; i < res.result.length; i++) {
            let temp = res.result[i];
            that.departTree.push(temp);
          }
        }

      })
    },
    add (depart) {
      if(depart){
        this.seen = false;
        this.dictDisabled = false;
      }else{
        this.seen = true;
        this.dictDisabled = true;
      }
      this.edit(depart);
    },
    edit (record) {
      this.form.resetFields();
      this.model = Object.assign({}, {});
      this.visible = true;
      this.loadTreeData();
      this.model.parentId = record!=null?record.toString():null;
      
      let userinfo = Vue.ls.get(USER_INFO)       
      this.model.usccPath = userinfo.enterpriseUscc;
      
      if(this.seen){
        this.model.orgCategory = '1';
      }else{
        this.model.orgCategory = '2';
      }
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model,'enterpriseName','enterpeiseShort','parentId','enterpriseUscc','usccScancopy','enterpriseCode','contactPerson','contactPhone','enterpriseType','manageType','brands','registerAddr','operationAddr','isApproved','isSeparate','loginname','passcode','logintoken','createUser','updateUser','usccPath'))
      });
    },
    close () {
      this.$emit('close');
      this.disableSubmit = false;
      this.visible = false;
    },
    handleOk () {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let formData = Object.assign(this.model, values);
          //时间格式化
          console.log(formData)
          httpAction(this.url.add,formData,"post").then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.loadTreeData();
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })

        }
      })
    },
    handleCancel () {
      this.close()
    },
    validateMobile(rule,value,callback){
      if (!value || new RegExp(/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/).test(value)){
        callback();
      }else{
        callback("您的手机号码格式不正确!");
      }

    }
  }
}
</script>

<style scoped>

</style>